package com.example.mystoryapp2.ui.main.fragments

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.view.View
import com.example.mystoryapp2.R
import com.example.mystoryapp2.databinding.FragmentSettingBinding
import com.example.mystoryapp2.ui.base.BaseFragment
import com.example.mystoryapp2.ui.story.storylist.StoryListActivity
import com.example.mystoryapp2.ui.utils.DialogUtils
import com.example.mystoryapp2.ui.utils.UserInfoUtils

class SettingFragment : BaseFragment<FragmentSettingBinding>() {
    private lateinit var viewModel: SettingViewModel

    companion object {
        fun newInstance() = SettingFragment()
    }

    override fun onInitView(view: View) {
        mDataBinding.btnLogout.setOnClickListener { logout() }
        if (mDataBinding.viewModel?.isAuthor() == true) {
            mDataBinding.btnCollect.visibility = View.GONE
        }
        mDataBinding.btnCollect.setOnClickListener { collectList() }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this)[SettingViewModel::class.java]
        mDataBinding.viewModel = viewModel
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_setting
    }

    private fun logout() {
        activity?.let {
            this.context?.let { it1 ->
                DialogUtils.showConfirmDialog(it1) {
                    UserInfoUtils.backToLogin(it)
                }
            }
        }
    }

    private fun collectList() {
        activity?.let { StoryListActivity.startCollected(it) }
    }
}